/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jxsm.cz.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jxsm.cz.biz.BizBannerInfo;
import com.jxsm.cz.biz.BizScoreActivitiesInfo;
import com.jxsm.cz.model.BannerInfo;
import com.jxsm.cz.model.ScoreActivitiesInfo;
import com.jxsm.config.Message;
import com.jxsm.jxbrms.model.SystemDict;
import com.jxsm.jxbrms.query.QueryController;
import com.jxsm.web.upload.JUploadFileInfo;
import com.jxsm.web.upload.JUploadInfo;
import java.math.BigInteger;
import java.util.List;

/**
 *
 * @author lenovo
 */
public class ScoreActivitiesInfoController extends QueryController {

    public void added() {
        BizScoreActivitiesInfo bizScoreExercise = new BizScoreActivitiesInfo();
        ScoreActivitiesInfo se = this.getModel(ScoreActivitiesInfo.class);
        //开始时间必须大于结束时间校验
        long start_time = se.getLong("start_time");
        long end_time = se.getLong("end_time");
        if (start_time >= end_time) {
            this.setMessage(new Message(2, "0", "开始时间必须小于结束时间!"));
            this.renderMessage();
            return;
        }
        System.out.println("传入的积分类型"+se.getStr("score_type"));
        //在同一时间段针对同一种积分类型只能存在一种活动校验
        //查询传入的时间段内是否存在其他当前类型的积分活动
        ScoreActivitiesInfo scoreExercise = ScoreActivitiesInfo.dao.findFirstByWhere(" start_time > ? or end_time < ? and score_type = ?", se.getLong("end_time"), se.getLong("start_time"), se.getStr("score_type"));
        SystemDict systemDict = SystemDict.dao.findFirstByWhere("dicttype_id = 'score_type' and dict_code = ? ", se.getStr("score_type"));
        String str = "当前时间段内已经存在其他" + systemDict.getStr("dict_name") + "积分活动!";
        if (scoreExercise != null) {
            this.setMessage(new Message(2, "0", str));
            this.renderMessage();
            return;
        }
        bizScoreExercise.addTx(se);
        if (bizScoreExercise.isSuccess()) {
            this.renderAddSuccess(se.getStr("exercise_name"));
        } else {
            this.renderAddFail(bizScoreExercise.getErrorInfo(), se.getStr("exercise_name"));
        }
    }

    public void edited() {
        BizScoreActivitiesInfo bizScoreExercise = new BizScoreActivitiesInfo();
        ScoreActivitiesInfo se = this.getModel(ScoreActivitiesInfo.class);
        //开始时间必须大于结束时间校验
        //在同一时间段针对同一种积分类型只能存在一种活动校验
        //开始时间必须大于结束时间校验
        long start_time = se.getLong("start_time");
        long end_time = se.getLong("end_time");
        if (start_time >= end_time) {
            this.setMessage(new Message(2, "0", "开始时间必须小于结束时间!"));
            this.renderMessage();
            return;
        }
        //在同一时间段针对同一种积分类型只能存在一种活动校验
        //查询传入的时间段内是否存在其他当前类型的积分活动
        ScoreActivitiesInfo scoreExercise = ScoreActivitiesInfo.dao.findFirstByWhere(" start_time > ? or end_time < ? and score_type = ? and id <> ?", se.getLong("end_time"), se.getLong("start_time"), se.getStr("score_type"), se.get("id"));
        SystemDict systemDict = SystemDict.dao.findFirstByWhere("dicttype_id = 'score_type' and dict_code = ? ", se.getStr("score_type"));
        String str = "当前时间段内已经存在其他" + systemDict.getStr("dict_name") + "积分活动!";
        if (scoreExercise != null) {
            this.setMessage(new Message(2, "0", str));
            this.renderMessage();
            return;
        }
        bizScoreExercise.editTx(se);
        if (bizScoreExercise.isSuccess()) {
            this.setMessage(new Message(0, "0", "信息更新成功"));
            this.renderMessage();
        } else {
            this.setMessage(new Message(2, "0", "信息更新失败!"));
            this.renderMessage();
        }
    }

    @Override
    public void editing() {
        String id = this.getPara(0);
        ScoreActivitiesInfo bi = ScoreActivitiesInfo.dao.findById(id);
        this.setAttr("scoreExercise", bi);
        super.editing();
    }

    public void delete() {
        BizScoreActivitiesInfo bizScoreExercise = new BizScoreActivitiesInfo();
        bizScoreExercise.deleteTx(this.getPara(0));
        if (bizScoreExercise.isSuccess()) {
            this.setMessage(new Message(0, "0", "删除成功"));
            this.renderMessage();
        } else {
            this.setMessage(new Message(2, "0", "删除失败!"));
            this.renderMessage();
        }
    }
}
